package module;

import Client.ClientInitClose;
import Client.ClientScanner;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class StudentExam {

    private static ClientInitClose client;
    private static List<ExamQuestions> examQuestions = new ArrayList<>();


    public void begainExam(String num) throws IOException {
        int score = 0;
        int i=0;
        for (ExamQuestions examQuestions : examQuestions){
            System.out.println("第"+(i+1)+"题:");
            System.out.println(examQuestions.getTitle());
            System.out.println(examQuestions.getChoose());
            String str = ClientScanner.getScanner().next();
            if("bye".equals(str)){
                client.getOut().writeObject("bye");
                System.out.println("考试终止");
                return;
            }else if(examQuestions.getAnswer().equals(str)){
                System.out.println("回答正确！");
                score +=10;
            }
        }
        System.out.println("最后的成绩为："+score);
        client.getOut().writeObject(num+";"+score);
    }


    public void studentExam(ClientInitClose c,String num) throws IOException, ClassNotFoundException {

        client = c;

        while (true) {
            System.out.println("-----考试模块-----");
            System.out.println("  1、开始考试");
            System.out.println("  2、查询成绩");
            System.out.println("  3、导出成绩");
            System.out.println("  0、退出系统");
            int choose = ClientScanner.getScanner().nextInt();
            switch (choose){
                case 1:
                    System.out.println("获取考题中...");
                    client.getOut().writeObject("beg");
                    examQuestions = (List<ExamQuestions>) client.getIn().readObject();
                    begainExam(num);
                    break;
                case 2:
                    client.getOut().writeObject("find"+";"+num);
                    System.out.println((String) client.getIn().readObject());
                    break;
                case 3:
                    client.getOut().writeObject("all");
                    List<Student> students = new ArrayList<>();
                    students = (List<Student>)client.getIn().readObject();
                    ObjectOutputStream objectOutput = new ObjectOutputStream(new FileOutputStream("d:/test/listStudents1.txt"));
                    objectOutput.writeObject(students);
                    System.out.println("导出成功！");
                    break;
                case 0:
                    client.getOut().writeObject("end");
                    return;
                default:
                    System.out.println("请重新选择0-3的功能");
                    break;
            }
        }
    }
}
